package com.itheima.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //添加套餐
    @Override
    public R add(SetmealDto setmealDto) {
        //1.保存到套餐表中
        setmealMapper.insert(setmealDto);
        //2.保存套餐菜品信息到中间表
        //2.1把所有的套餐菜品细腻些取出来
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //2.2保存到数据库
        for (SetmealDish setmealDish : setmealDishes) {
            //1.添加套餐id
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("添加成功");

    }

    //分页查询
    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        //根据分页条件查询套餐信息
        IPage<Setmeal> p = new Page<>();
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (name != null){
            wrapper.like(Setmeal::getName,name);
        }
        IPage<Setmeal> pageInfo = setmealMapper.selectPage(p, wrapper);

        //2.把setmeal 转换成 setmealDto
        //.把把setmeal集合转换成 setmealDto集合
        List<Setmeal> setmeals = pageInfo.getRecords();
        String jsonString = JSON.toJSONString(setmeals);
        List<SetmealDto> setmealDtos = JSON.parseArray(jsonString, SetmealDto.class);

        //给setmealDtos 设置分类名称
        for (SetmealDto setmealDto : setmealDtos) {
            String categoryName = categoryMapper.selectById(setmealDto.getCategoryId()).getName();
            setmealDto.setCategoryName(categoryName);
        }
        //3.封装再Page对象中
        String s = JSON.toJSONString(pageInfo);
        Page page1 = JSON.parseObject(s, Page.class);
        page1.setRecords(setmealDtos);
        return R.success(page1);
    }
    //批量删除套餐
    @Override
    public void removeIds(Long[] ids) {
        //删除套餐
        setmealMapper.deleteBatchIds(Arrays.asList(ids));

        //删除套餐菜品信息
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper);
    }
    //批量起售停售
    @Override
    public R modyfiStatus(Long[] ids, Integer status) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus,status).in(Setmeal::getId,ids);
        setmealMapper.update(setmeal,wrapper);
        return R.success("批量修改成功");
    }
    //套餐回显
    @Override
    public R findById(Long id) {
        //回显setmeal数据
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询出套餐里菜品数据
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        //把setmeal转换成setmealDto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //把菜品数据封装到setmealDto并返回
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }
    //修改保存功能  用先删后添方法
    @Override
    public R modyfi(SetmealDto setmealDto) {
        //先把setmealDto转换成setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        //修改套餐数据
        setmealMapper.updateById(setmeal);
        //找出该套餐对应的菜品,并删除
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //循环菜品集合 并循环添加
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("修改成功");
    }
    //根据分类id查询套餐列表
    @Override
    public R list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper);
        return R.success(setmeals);
    }
}
